﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BOSS.Domain.Security;
using BOSS.Dao.Security;

namespace BOSS.Dao.NHibernate.Security
{
    /// <summary>
    /// 帐号权限设置 DAO
    /// </summary>
    public class AccountPrivilegeSettingDao : EntityDao<AccountPrivilegeSetting, int>, IAccountPrivilegeSettingDao
    {
        #region IAccountPrivilegeSettingDao Members

        /// <summary>
        /// 获取指定帐号的权限集合
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public PrivilegeGroup SelectByAccount(Account account)
        {
            IList<AccountPrivilegeSetting> settings = this.HibernateTemplate.Find<AccountPrivilegeSetting>(
                "from AccountPrivilegeSetting s where s.Account = ?", account);
            PrivilegeGroup privilegeGroup = new PrivilegeGroup();
            foreach (AccountPrivilegeSetting setting in settings)
            {
                privilegeGroup.AddPrivilege(new Privilege(setting.PrivilegeName, setting.Permission));
            }
            return privilegeGroup;
        }

        /// <summary>
        /// 为指定帐号添加权限
        /// </summary>
        /// <param name="account"></param>
        /// <param name="privilege"></param>
        /// <returns></returns>
        public int Insert(Account account, Privilege privilege)
        {
            return (int)this.HibernateTemplate.Save(new AccountPrivilegeSetting()
            {
                Account = account,
                PrivilegeName = privilege.Name,
                Permission = privilege.Permission
            });
        }

        /// <summary>
        /// 为指定帐号更新权限
        /// </summary>
        /// <param name="account"></param>
        /// <param name="privilege"></param>
        public void Update(Account account, Privilege privilege)
        {
            AccountPrivilegeSetting setting = this.HibernateTemplate.Find<AccountPrivilegeSetting>(
               "from AccountPrivilegeSetting s where s.Account = ? and s.PrivilegeName = ?", new object[] { account, privilege.Name })
               .FirstOrDefault();
            if (setting != null)
            {
                setting.Permission = privilege.Permission;
                this.HibernateTemplate.Update(setting);
            }
        }

        /// <summary>
        /// 为指定帐号移除权限
        /// </summary>
        /// <param name="account"></param>
        /// <param name="privilege"></param>
        public void Delete(Account account, Privilege privilege)
        {
            AccountPrivilegeSetting setting = this.HibernateTemplate.Find<AccountPrivilegeSetting>(
            "from AccountPrivilegeSetting s where s.Account = ? and s.PrivilegeName = ?", new object[] { account, privilege.Name })
            .FirstOrDefault();
            if (setting != null)
            {
                this.HibernateTemplate.Delete(setting);
            }
        }

        #endregion
    }
}
